package cn.wolfcode.crm.serivce.impl;

import cn.wolfcode.crm.domain.SpendClassify;
import cn.wolfcode.crm.mapper.SpendClassifyMapper;
import cn.wolfcode.crm.serivce.ISpendClassifyService;
import cn.wolfcode.crm.util.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Service
public class SpendClassifyServiceImpl implements ISpendClassifyService {
    @Autowired
    private SpendClassifyMapper mapper;


    @Override
    public void deleteByPrimaryKey(Long id) {
        //查询子分类
        List<SpendClassify> spendChildrenClassify = mapper.selectChildrenSpendClaaify(id);
        //如果有子分类,则不能删除
        if(spendChildrenClassify.size()!=0){
            throw new RuntimeException("我有子分类,您不能删除我哦!");
        }
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public void saveOrUpdate(SpendClassify record) {
        if (record.getId() == null) {
            //保存操作
            mapper.insert(record);
        }else {
            mapper.updateByPrimaryKey(record);
        }
    }

    @Override
    public SpendClassify selectByPrimaryKey(Long id) {
        return mapper.selectByPrimaryKey(id);
    }



    @Override
    public List<SpendClassify> selectAll() {
        return mapper.selectAll();
    }

    @Override
    public List<SpendClassify> selectRootClassify() {
        return mapper.selectRootClassify();
    }

    //查询子分类
    @Override
    public List<SpendClassify> selectChildrenSpendClaaify(Long parent_id) {
        return mapper.selectChildrenSpendClaaify(parent_id);
    }

    @Override
    public void saveSpendParent(String text) {
        mapper.saveSpendParent(text);
    }

    @Override
    public void saveSpendChildren(String text,Long id) {
        mapper.saveSpendChildren(text,id);
    }


}
